
package com.zjff.myshop.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zjff.myshop.front.pojo.form.OrderInsertForm;
import com.zjff.myshop.front.pojo.form.OrderQueryListForm;
import com.zjff.myshop.front.pojo.form.OrderUpdateStatusForm;
import com.zjff.myshop.front.pojo.vo.OrderDetailVO;

import com.zjff.myshop.front.service.OrderService;
import com.zjff.myshop.util.*;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Api(value = "v1", tags = "07.订单模块")
@RequestMapping("/app/v1/order")
public class OrderController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Resource
    private OrderService orderService;


    /**
     * 增加订单（从商品列表创建）
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "增加订单（从商品列表创建建）", notes = "从商品列表创建新的订单，接口参数：地址id、商品列表")
    @ApiImplicitParam(name = "form", value = "增加订单参数", dataType = "OrderInsertForm", required = true)
    @SaCheckLogin
    @PostMapping("/insert")
    public Result<Integer> insert(@Valid @RequestBody OrderInsertForm form) {
        //保存订单并返回订单号
        Integer orderId = orderService.insert(form);
        if (orderId > 0) {
            return Result.ok(orderId);
        }
        return Result.error();
    }

    /**
     * 更改订单状态(0待支付/1已支付/2已发货/3交易成功/-1已取消)
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "更改订单状态", notes = "更改订单状态(0待支付/1已支付/2已发货/3交易成功/-1已取消)")
    @ApiImplicitParam(name = "form", value = "订单状态参数", dataType = "OrderUpdateStatusForm", required = true)
    @SaCheckLogin
    @PostMapping("/updateStatus")
    public Result updateStatus(@Valid @RequestBody OrderUpdateStatusForm form) {
        if (orderService.updateStatus(form.getId(), form.getStatus())) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    /**
     * 查询订单详情
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "查询订单详情", notes = "返回订单id对应的订单详情")
    @ApiImplicitParam(name = "orderId", value = "订单id", required = true)
    @SaCheckLogin
    @GetMapping("/{orderId}")
    public Result<OrderDetailVO> selectDetailById(@PathVariable("orderId") Integer orderId) {

        logger.info("url={} \n token={}", SaHolder.getRequest().getUrl(), StpUtil.getTokenInfo());

        OrderDetailVO orderDetailVO = orderService.selectDetailById(orderId);
        return Result.ok(orderDetailVO);
    }

    /**
     * 查询我的订单列表
     */
    @ApiOperationSupport(order = 31)
    @ApiOperation(value = "查询我的订单列表", notes = "返回我的订单列表，接口参数：状态")
    @ApiImplicitParam(name = "form", value = "订单查询分页参数", dataType = "OrderQueryListForm", required = true)
    @SaCheckLogin
    @PostMapping("/getMyList")
    public Result<List<OrderDetailVO>> getMyList(@Valid @RequestBody OrderQueryListForm form) {
        //form中有参数 status
        Integer userId = StpUtil.getLoginIdAsInt();
        Map<String, Object> map = BeanUtils.beanToMap(form);
        map.put("userId", userId);  //指定用户id是当前登录用户
        List<OrderDetailVO> list = orderService.selectDetailVOList(map);
        return Result.ok(list); //返回的数据是列表
    }
}